#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
from pathlib import Path

class OpcrBinInstaller:
  def __init__(self):
    self.shell = os.environ.get("SHELL")
    self.dir = Path(__file__).parent.absolute()

  def install(self):
    if 'bash' in self.shell:
      self.install_bash()
    elif 'zsh' in self.shell:
      self.install_zsh()
  
  def install_bash(self):
    self.entry_file = "opcr-bin.sh"
    self.profile = './test/test.bashrc'
    if self.has_installed():
      print("{0} already in {1}".format(self.entry_file, self.profile))
    else:
      with open(self.profile, "a") as f:
        f.write("\n")
        f.write("export PATH=$PATH:{0}\n".format(self.dir))
        f.write("source {0}\n".format(self.entry_path()))
        f.write("\n")
    print(". {0} # to reload ".format(self.entry_path()))

  def entry_path(self):
    return os.path.join(self.dir, self.entry_file)

  def has_installed(self):
    with open(self.profile, 'r') as file:
      return self.entry_file in file.read()

  def do(self):
    print("install to " + self.shell)
    self.install()

OpcrBinInstaller().do()